/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePass.java
 *
 * Created on Aug 28, 2010, 8:01:22 AM
 */

package GUI;

import BusinessLogic.*;
import javax.swing.JOptionPane;

/**
 *
 * @author MrToan
 */
public class ChangePass extends javax.swing.JFrame {

    /** Creates new form ChangePass */
    private User user;
    public ChangePass(User userObj) {
        user = userObj;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblOld = new javax.swing.JLabel();
        lblNew = new javax.swing.JLabel();
        txtOldPass = new javax.swing.JPasswordField();
        txtNewPass = new javax.swing.JPasswordField();
        bntChange = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        lblRetype = new javax.swing.JLabel();
        txtRetypePass = new javax.swing.JPasswordField();

        setTitle("Change Password");

        lblOld.setText("Old Password :");

        lblNew.setText("New Password :");

        bntChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Edit-icon.png"))); // NOI18N
        bntChange.setText("Change");
        bntChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntChangeActionPerformed(evt);
            }
        });

        bntCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Undo-icon.png"))); // NOI18N
        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        lblRetype.setText("Re-type New Pass:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(bntChange)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                        .addComponent(bntCancel)
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblOld)
                            .addComponent(lblNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblRetype, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRetypePass, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                            .addComponent(txtNewPass, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                            .addComponent(txtOldPass, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))))
                .addGap(58, 58, 58))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtOldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOld))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNew)
                    .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRetype)
                    .addComponent(txtRetypePass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntChange)
                    .addComponent(bntCancel))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntChangeActionPerformed
        // TODO add your handling code here:
        System.out.println(""+user.getPassword());
        if(ValidateForm()==1)
        {
            String message = new AdminFunction().changePass_BL(user.getUserID(),NewPass);
            JOptionPane.showMessageDialog(this, message);
            this.dispose();
        }
    }//GEN-LAST:event_bntChangeActionPerformed

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_bntCancelActionPerformed

    //method Validate Password Form
    public int ValidateForm()
    {
        char[] Old=txtOldPass.getPassword();
        char[] New=txtNewPass.getPassword();
        char[] retypeNew = txtRetypePass.getPassword();
        OldPass=String.valueOf(Old);
        NewPass=String.valueOf(New);
        Retype = new String(retypeNew);
        if(OldPass.equals(""))
        {
            JOptionPane.showMessageDialog(this,"Enter Old Password !","Error !",2,null);
            txtOldPass.requestFocus();
            return 0;
        }
        else if(NewPass.equals(""))
        {
            JOptionPane.showMessageDialog(this,"Enter New Password !","Error !",2,null);
            txtNewPass.requestFocus();
            return 0;
        }
        else if(Retype.equals("")){
            JOptionPane.showMessageDialog(this,"Enter Retype Password !","Error !",2,null);
            txtRetypePass.requestFocus();
            return 0;
        }else if(!Retype.equals(NewPass))
        {
            JOptionPane.showMessageDialog(this,"NewPass and RetypePass isn't the same!","Error !",2,null);
            return 0;
        }else if(!OldPass.equals(user.getPassword())){
            JOptionPane.showMessageDialog(this,"Wrong OldPass !","Error !",2,null);
            txtOldPass.requestFocus();
            return 0;
        }
        return 1;
    }

    private String OldPass;
    private String NewPass;
    private String Retype;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntChange;
    private javax.swing.JLabel lblNew;
    private javax.swing.JLabel lblOld;
    private javax.swing.JLabel lblRetype;
    private javax.swing.JPasswordField txtNewPass;
    private javax.swing.JPasswordField txtOldPass;
    private javax.swing.JPasswordField txtRetypePass;
    // End of variables declaration//GEN-END:variables

}
